package com.rabbittk.cameraxdemo;

import android.content.Context;
import android.opengl.GLES11Ext;
import android.opengl.GLES20;

import com.rabbittk.cameraxdemo.R;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class BGScreenFilter {

    private static final String TAG = "BGScreenFilter";
    private Context mContext;
    private int vPosition;
    private int vCoords;
    private int vTexture;
    private int vMatrix;
    private float[] mtx = new float[16];
    private int mWidth = 0;
    private int mHeight = 0;

    private FloatBuffer fragmentBuffer;
    private FloatBuffer vertexBuffer;
    // private val temperature: Float = 5050.0f

    //顶点坐标
    private float[] VERTEXCOORDS = {
            -1.0f, -1.0f,
            1.0f, -1.0f,
            -1.0f, 1.0f,
            1.0f, 1.0f
    };

    //纹理坐标
    private float[] FRAGMENTCOORDS = {
            0.0f, 0.0f,
            1.0f, 0.0f,
            0.0f, 1.0f,
            1.0f, 1.0f
            //        1f, 1f,
            //        1f, 0f,
            //        0f, 1f,
            //        0f, 0f
    };

    private int program;

    public BGScreenFilter(Context context) {
        mContext = context;
        init();
    }

    private void init() {
        vertexBuffer = ByteBuffer.allocateDirect(4 * 4 * 2)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        vertexBuffer.clear();
        vertexBuffer.put(VERTEXCOORDS);

        fragmentBuffer = ByteBuffer.allocateDirect(4 * 2 * 4)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        fragmentBuffer.clear();
        fragmentBuffer.put(FRAGMENTCOORDS);

        String vertexShader = com.rabbittk.cameraxdemo.OpenGLUtils.readRawTextFile(mContext, R.raw.camera_vert);
        String fragmentShader = com.rabbittk.cameraxdemo.OpenGLUtils.readRawTextFile(mContext, R.raw.camera_frag);

        program = com.rabbittk.cameraxdemo.OpenGLUtils.loadProgram(vertexShader, fragmentShader);
        GLES20.glUseProgram(program);

        vPosition = GLES20.glGetAttribLocation(program, "vPosition");
        vCoords = GLES20.glGetAttribLocation(program, "vCoord");
        vTexture = GLES20.glGetUniformLocation(program, "vTexture");
        vMatrix = GLES20.glGetUniformLocation(program, "vMatrix");


        vertexBuffer.position(0);
        GLES20.glVertexAttribPointer(vPosition, 2, GLES20.GL_FLOAT, false, 0, vertexBuffer);
        GLES20.glEnableVertexAttribArray(vPosition);
        fragmentBuffer.position(0);
        GLES20.glVertexAttribPointer(vCoords, 2, GLES20.GL_FLOAT, false, 0, fragmentBuffer);
        GLES20.glEnableVertexAttribArray(vCoords);

    }

    public void onDrawFrame(int textureId) {
        GLES20.glUniformMatrix4fv(vMatrix, 1, false, mtx, 0);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, textureId);
        /* x取值： 0: GL_TEXTURE0~GL_TEXTURE31+64; n: GL_TEXTUREn(n取值1~95) */
        GLES20.glUniform1i(vTexture, 0);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
    }

    public void setTransformMatrix(float[] mtx) {
        this.mtx = mtx;
    }

    public void onReady(int width, int height) {

    }
}
